package com.cosina.util.ep.findjar.view;


import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;


public class FindJarView extends ViewPart {

	public static final String ID = "com.cosina.util.ep.findjar.view.FindJarView";

	public FindJarView() {
	}

	
	private Text classNameText, folderText, result; 
	private Button searchButton;
	public void createPartControl(final Composite parent) {
		Composite content = new Composite(parent, SWT.NONE);
		content.setBackground(new Color(parent.getDisplay(), 50, 50, 50));
		content.setLayoutData(new FillLayout());
		GridLayout gl = new GridLayout(2, false);
		content.setLayout(gl);
		
		
		new Label(content, SWT.NONE).setText("ClassName");
		classNameText = new Text(content,SWT.NONE);
		{
			GridData gd = new GridData(240,24);
			classNameText.setLayoutData(gd);
		}
		
		Button getFolderButton = new Button(content, SWT.NONE);
		getFolderButton.setText("Folder");
		getFolderButton.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog (parent.getShell());
				String result = dialog.open();
				if(null != result){
					folderText.setText(result);
				}
			}
		});
		folderText = new Text(content,SWT.NONE);
		{
			GridData gd = new GridData(240,24);
			folderText.setLayoutData(gd);
		}
		
		
		searchButton = new Button(content, SWT.NONE);
		searchButton.setText("GoFind");
		searchButton.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				try{
					result.setText("");
					File findResult = FindJarViewTest.find(classNameText.getText(), folderText.getText());
					if(null != result){
						result.setText(findResult.getName());
					}
				}catch(Exception ex){
					result.setText("can't find:" + ex.getMessage());
				}
			}
		});
		
		result = new Text(content,SWT.NONE);
		{
			GridData gd = new GridData(240,24);
			result.setLayoutData(gd);
		}
	}
	
	public void setFocus() {
	}
}